using GHIElectronics.NETMF.Glide.UI;

namespace Melody_Glider.GUIContent
{
    class NumericalInputWindow: WindowFrame
    {
        private readonly GUI _gui;
        private readonly SystemState _conditions;
        private bool _firstChange;
        private int _tempValue;

        public NumericalInputWindow(GUI gui, SystemState state)
        {
            _gui = gui;
            _conditions = state;
        }

        protected override void InitButtons()
        {
            for (var i = 0; i <= 9; i++)
            {
                var numBtn = (Button)ThisWindow.GetChildByName("Button_" + i);
                numBtn.TapEvent += NumBtnTapEvent;
            }

            var okBtn = (Button)ThisWindow.GetChildByName("OKButton");
            okBtn.TapEvent += OkBtnTapEvent;

            var cancelBtn = (Button)ThisWindow.GetChildByName("CancelButton");
            cancelBtn.TapEvent += CancelBtnTapEvent;

            var clrBtn = (Button)ThisWindow.GetChildByName("Button_Clear");
            clrBtn.TapEvent += ClrBtnTapEvent;

            var bkspBtn = (Button)ThisWindow.GetChildByName("Button_Backspace");
            bkspBtn.TapEvent += BkspBtnTapEvent;
        }

        private void NumBtnTapEvent(object sender)
        {
            sender.GetType();
            var tappedOne = (Button)sender;
            var tappedNumber = int.Parse(tappedOne.Name.Substring(7));

            if (_conditions.BaseOctaveMenu) //for base octave input
            {
                _tempValue = tappedNumber;
            }
            else //for bpm input
            {
                if (!_firstChange)
                {
                    if (_tempValue.ToString().Length == 3)
                        return;
                    if (_tempValue*10+tappedNumber >= 1000)
                        return;

                    _tempValue = _tempValue*10+tappedNumber;
                }
                else
                {
                    _tempValue = tappedNumber;

                    _firstChange = false;
                }
            }
            Update();
        }

        private void OkBtnTapEvent(object sender)
        {
            if (_conditions.BaseOctaveMenu)
            {
                _conditions.MelodyBuffer.SetBaseOctave((byte)_tempValue);
                _conditions.NewBaseOctave = false; //todo: this may be set many times unnecessarily
            }
            else
            {
                _conditions.MelodyBuffer.SetBpm(_tempValue);
                _conditions.NewBpm = false;
            }

            _conditions.IsFileSaved = false;
            _gui.EditorWindow.SetToMain();
        }

        private void CancelBtnTapEvent(object sender)
        {
            _gui.EditorWindow.SetToMain();
            _gui.ToggleElement("OKButton", ThisWindow, true); //if user pressed 0 and cancel after that
        }

        private void ClrBtnTapEvent(object sender)
        {
            if (_tempValue.ToString().Length == 0)
                return;

            _tempValue = 0;

            Update();
        }

        private void BkspBtnTapEvent(object sender)
        {
            if (_tempValue.ToString().Length == 0)
                return;

            _tempValue = _tempValue / 10;

            Update();
        }

        private void Update()
        {
            var valueText = (TextBox)ThisWindow.GetChildByName("ValueBox");
            valueText.Text = _tempValue.ToString();
            valueText.Invalidate();

            if (_tempValue != 0)
                _gui.ToggleElement("OKButton", ThisWindow, true);
            if (_tempValue == 0)
                _gui.ToggleElement("OKButton", ThisWindow, false);
        }

        protected override void PrepareToMain()
        {
            var valueText = (TextBox)ThisWindow.GetChildByName("ValueBox");

            if (_conditions.BaseOctaveMenu)
            {
                if (_conditions.NewBaseOctave)
                {
                    valueText.Text = "4";
                    _tempValue = 4;
                }
                else
                {
                    valueText.Text = _conditions.MelodyBuffer.GetBaseOctave().ToString();
                    _tempValue = _conditions.MelodyBuffer.GetBaseOctave();
                }

                valueText.Invalidate();

                DisableButtons();
            }
            else
            {
                if (_conditions.NewBpm)
                {
                    valueText.Text = "120";
                    _tempValue = 120;
                }
                else
                {
                    valueText.Text = _conditions.MelodyBuffer.GetBpm().ToString();
                    _tempValue = _conditions.MelodyBuffer.GetBpm();
                }

                valueText.Invalidate();
                
                EnableButtons();
            }

            _firstChange = true;
        }

        private void EnableButtons()
        {
            _gui.ToggleElement("Button_Clear", ThisWindow, true); //todo: create overload of ToggleElement for string array
            _gui.ToggleElement("Button_Backspace", ThisWindow, true);
            _gui.ToggleElement("Button_0", ThisWindow, true);
            _gui.ToggleElement("Button_9", ThisWindow, true);
        }

        private void DisableButtons()
        {
            _gui.ToggleElement("Button_Clear", ThisWindow, false);
            _gui.ToggleElement("Button_Backspace", ThisWindow, false);
            _gui.ToggleElement("Button_0", ThisWindow, false);
            _gui.ToggleElement("Button_9", ThisWindow, false);
        }
    }
}
